﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using Microsoft.International.Converters.PinYinConverter;

namespace YSLPages
{
    public class KPinYinConverter
    {
        /// <summary>
        /// 汉字转拼音
        /// </summary>
        /// <param name="ChineseStr"></param>
        /// <param name="Separator"></param>
        /// <returns></returns>
        public static string ConvertToPinYin(
            string ChineseStr,
            string Separator = "",
            MultiPinYinSelMode SelMode = MultiPinYinSelMode.FirstOne,
            DelSelectMultiPinYin SelectPinYin = null)
        {
            try
            {
                List<string> ltAllChinese = new List<string>();
                ChinesePinyin WordPinYin;
                foreach (char crNow in ChineseStr)
                {
                    WordPinYin = new ChinesePinyin(crNow);
                    if (WordPinYin.PinYins.Count == 1)//仅一个拼音
                        ltAllChinese.Add(WordPinYin.PinYins[0]);
                    else if (WordPinYin.PinYins.Count > 1)//多音字选择
                    {
                        if (SelMode == MultiPinYinSelMode.FirstOne)//第一个                        
                            ltAllChinese.Add(WordPinYin.PinYins[0]);
                        else if (SelMode == MultiPinYinSelMode.EmptyString)//空字符串                        
                            ltAllChinese.Add("");
                        else if (SelMode == MultiPinYinSelMode.ManualSel)//手动选择
                            ltAllChinese.Add(SelectPinYin != null ? SelectPinYin(WordPinYin) : "");
                    }
                }
                return string.Join(Separator, ltAllChinese);
            }
            catch
            {
                return "";
            }
        }
    }


    /// <summary>
    /// 多拼音选择委托
    /// </summary>
    /// <param name="AllPinYins"></param>
    /// <returns></returns>
    public delegate string DelSelectMultiPinYin(ChinesePinyin AllPinYins);

    /// <summary>
    /// 多拼音选择模式
    /// </summary>
    public enum MultiPinYinSelMode
    {
        /// <summary>
        /// 选择第一个
        /// </summary>
        FirstOne,
        /// <summary>
        /// 空字符串
        /// </summary>
        EmptyString,
        /// <summary>
        /// 手动选择
        /// </summary>
        ManualSel
    }

    /// <summary>
    /// 中文拼音
    /// </summary>
    public class ChinesePinyin
    {
        public ChinesePinyin(char ChineseWord)
        {
            this.ChineseWord = ChineseWord;
        }

        /// <summary>
        /// 中文字                       
        /// </summary>
        public char ChineseWord;

        /// <summary>
        /// 所有拼音
        /// </summary>
        private List<string> _PinYins;
        public List<string> PinYins
        {
            get
            {
                try
                {
                    if (_PinYins == null)
                    {
                        //当前中文字符的所有拼音
                        ReadOnlyCollection<string> NowPinYins = new ChineseChar(ChineseWord).Pinyins;
                        string RightStr;

                        _PinYins = new List<string>();
                        for (int i = 0; i < NowPinYins.Count && NowPinYins[i] != null; i++)
                        {
                            //去掉末尾的数字
                            RightStr = "";
                            foreach (char cr in NowPinYins[i])
                            {
                                if (char.IsDigit(cr))
                                    break;
                                RightStr += cr.ToString();
                            }
                            RightStr = RightStr.Replace("V", "YU"); 
                            //添加到临时拼音集
                            if (!_PinYins.Contains(RightStr))
                                _PinYins.Add(RightStr);
                        }
                    }

                    return _PinYins;
                }
                catch
                {
                    return null;
                }
            }
        }
    }
}
